package me.yuzho.study.concurrency;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import static net.mindview.util.Print.*;

public class Philosopher implements Runnable {
    
    private Chopstick left;
    private Chopstick right;
    
    private final int id;
    private final int ponderFactor;
    
    private Random rand = new Random(47);
    
    public Philosopher(Chopstick left, Chopstick right, int ident, int ponder) {
        this.left = left;
        this.right = right;
        
        id = ident;
        ponderFactor = ponder;
    }
    
    private void pause() throws InterruptedException {
        if(ponderFactor == 0) return;
        TimeUnit.MILLISECONDS.sleep(rand.nextInt(ponderFactor * 250));
    }

    @Override
    public String toString() {
        return String.format("Philosopher %d", id);
    }



    @Override
    public void run() {
        try {
            
            while(!Thread.interrupted()) {
                print(this + " " + "thinking");
                pause();
                
                // philosopher becomes hungry
                print(this + " " + "grabbing right");
                right.take();
                
                print(this + " " + "grabbing left");
                left.take();
                
                print(this + " " + "eating");
                pause();
                
                right.drop();
                left.drop();
            }
            
        } catch(InterruptedException e) {
            print(this + " " + "exiting via interrupt");
        }
        
    }
}
